<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="症状名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['symptomName']" placeholder="请输入症状名称"></a-input>
        </a-form-item>

        <!--<a-form-item label="部位选择" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
          <!--<a-select v-model="model.sex"-->
                    <!--placeholder="请选择性别" :allowClear="true" @change="getBodyPartsList">-->
            <!--<a-select-option :value="1">男</a-select-option>-->
            <!--<a-select-option :value="2">女</a-select-option>-->
          <!--</a-select>-->
          <!--<a-select v-model="model.type"-->
                    <!--placeholder="请选择类型" :allowClear="true" @change="getBodyPartsList">-->
            <!--<a-select-option :value="0">儿童</a-select-option>-->
            <!--<a-select-option :value="1">成人</a-select-option>-->
          <!--</a-select>-->

          <!--<a-select v-model="model.bodyPartsId"-->
                    <!--placeholder="请选择身体部位" :allowClear="true" @change="handleChange">-->
            <!--<a-select-option v-for="d in bodyPartsList"-->
                             <!--:key="d.id"-->
                             <!--:value="d.id">{{ d.bodyPartsName}}-->
            <!--</a-select-option>-->
          <!--</a-select>-->
        <!--</a-form-item>-->

        <a-form-item label="关联部位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            show-search
            option-filter-prop="children"
            :filter-option="filterOption"
            mode="multiple"
            v-model="bodyPartsInfoList"
            style="width: 100%"
            placeholder="关联部位"
            @change="handleChange"
          >
            <a-select-option v-for="item in bodyPartsList"
                             :value="item.id"
                             :key="item.id">
              {{item.sex == 1 ? "男":"女"}} -{{item.type == 0 ? "儿童" : "成人"}} - {{ item.bodyPartsName }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['sort']" placeholder="请输入排序"></a-input-number>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction , getAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'


  export default {
    name: "SmsBodyPartsSymptomModal",
    components: {
      JDate,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/sms/smsBodyPartsSymptom/add",
          edit: "/sms/smsBodyPartsSymptom/edit",
          getBodyPartsList: "/sms/smsBodyPartsInfo/getList",
        },
        bodyPartsList:[],
        bodyPartsInfoList:[],
      }
    },
    created () {
    },
    mounted() {
      this.getBodyPartsList();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.bodyPartsInfoList = [];
        if (this.model.bodyPartsInfoList && this.model.bodyPartsInfoList.length > 0){
          this.bodyPartsInfoList = this.model.bodyPartsInfoList;
        }
        this.getBodyPartsList();
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'symptomName','bodyPartsId','sort','createBy','createTime','updateBy','updateTime','delFlag','bodyPartsInfoList'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            this.model.bodyPartsInfoList = this.bodyPartsInfoList;
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'symptomName','bodyPartsId','sort','createBy','createTime','updateBy','updateTime','delFlag','bodyPartsInfoList'))
      },
      getBodyPartsList(){
        getAction(this.url.getBodyPartsList).then((res) => {
          if (res.success) {
            this.bodyPartsList = res.result;
            console.info(this.bodyPartsList)
          }
        });
      },
      handleChange(value) {
        this.departmentInfoList = value;
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
    }
  }
</script>